package com.quyou.funtour.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quyou.funtour.bean.ResultInfo;
import com.quyou.funtour.po.CarouselPo;
import com.quyou.funtour.po.NoticePo;
import com.quyou.funtour.service.CarouselService;
import com.quyou.funtour.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
@RequestMapping("/system")
@CrossOrigin
public class SystemController {

    @Autowired
    private CarouselService carouselService;
    @Autowired
    private NoticeService noticeService;

    /**
     * 添加轮播图
     * @return
     */
    @RequestMapping("/addCarousel")
    @ResponseBody
    public ResultInfo addCarousel(CarouselPo carouselPo){
        boolean save = carouselService.save(carouselPo);
        if(save){
            return ResultInfo.ok("添加成功");
        }else {
            return ResultInfo.no("添加失败");
        }
    }

    /**
     * 删除轮播
     * @param id
     * @return
     */
    @RequestMapping("/deleteCarousel/{id}")
    @ResponseBody
    public ResultInfo deleteCarousel(@PathVariable("id") String id){
        boolean removeById = carouselService.removeById(id);
        if(removeById){
            return ResultInfo.ok("删除成功");
        }else {
            return ResultInfo.no("删除失败");
        }
    }
    /**
     * 删除轮播
     * @return
     */
    @RequestMapping("/deleteCarouselByImg")
    @ResponseBody
    public ResultInfo deleteCarouselByImg(String img){
        boolean removeById = carouselService.remove(new QueryWrapper<CarouselPo>().eq("img",img));
        if(removeById){
            return ResultInfo.ok("删除成功");
        }else {
            return ResultInfo.no("删除失败");
        }
    }

    /**
     * 获取所有轮播
     * @return
     */
    @RequestMapping("/getCarouselList")
    @ResponseBody
    public ResultInfo getCarouselList(){
        List<CarouselPo> list = carouselService.list();
        return ResultInfo.ok(list);
    }

    /**
     * 添加公告
     * @return
     */
    @RequestMapping("/addNotice")
    @ResponseBody
    public ResultInfo addNotice(NoticePo noticePo){
        boolean save = noticeService.save(noticePo);
        if(save){
            return ResultInfo.ok("添加成功");
        }else {
            return ResultInfo.no("添加失败");
        }
    }

    /**
     * 删除公告
     * @param id
     * @return
     */
    @RequestMapping("/deleteNotice/{id}")
    @ResponseBody
    public ResultInfo deleteNotice(@PathVariable("id") String id){
        boolean removeById = noticeService.removeById(id);
        if(removeById){
            return ResultInfo.ok("删除成功");
        }else {
            return ResultInfo.no("删除失败");
        }
    }

    /**
     * 修改公告
     * @param noticePo
     * @return
     */
    @RequestMapping("/updateNotice")
    @ResponseBody
    public ResultInfo updateNotice(NoticePo noticePo){
        boolean removeById = noticeService.updateById(noticePo);
        if(removeById){
            return ResultInfo.ok("修改成功");
        }else {
            return ResultInfo.no("修改失败");
        }
    }

    /**
     * 根据id获取公告
     * @param id
     * @return
     */
    @RequestMapping("/getById/{id}")
    @ResponseBody
    public ResultInfo getById(@PathVariable("id") String id){
        NoticePo byId = noticeService.getById(id);
        if(byId!=null){
            return ResultInfo.ok(byId);
        }else {
            return ResultInfo.no("获取失败");
        }
    }

    /**
     * 获取所有公告
     * @return
     */
    @RequestMapping("/getNoticeList")
    @ResponseBody
    public ResultInfo getNoticeList(){
        List<NoticePo> list = noticeService.list();
        return ResultInfo.ok(list);
    }

}
